<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>表格查询</title>
    <#assign base=request.contextPath  />
    <base id="base" href="${base}">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, adminUser-scalable=0">
    <link rel="stylesheet" href="${base}/static/win10/component/layui/css/layui.css" media="all">
    <script>
        var base = "${base}";
    </script>
</head>
<body>
<br>


<div class="demoTable" style="margin-left: 15px;">
    <form class="layui-form " action="">
        <div class="layui-form-item">
            <label>问卷标题</label>
            <div class="layui-inline">
                <input class="layui-input" name="questionnaireTitle" id="questionnaireTitle" autocomplete="off">
            </div>
            <label>用户ID </label>
            <div class="layui-inline">
                <input class="layui-input" name="userId" id="userId" autocomplete="off">
            </div>
            <label>创建时间</label>
            <div class="layui-inline">
                <input class="layui-input" name="createTime" id="createTime" autocomplete="off">
            </div>
            <label>更新时间</label>
            <div class="layui-inline">
                <input class="layui-input" name="updateTime" id="updateTime" autocomplete="off">
            </div>
            <label>0正常 1停用</label>
            <div class="layui-inline">
                <input class="layui-input" name="delFlag" id="delFlag" autocomplete="off">
            </div>
            <label>0未发布/1发布/2停用</label>
            <div class="layui-inline">
                <input class="layui-input" name="status" id="status" autocomplete="off">
            </div>
            <label>问卷类型/1调查/2考试</label>
            <div class="layui-inline">
                <input class="layui-input" name="questionnaireType" id="questionnaireType" autocomplete="off">
            </div>

            <button type="button" class="layui-btn" data-type="reload">
                <i class="layui-icon layui-icon-search">搜索</i>
            </button>
        </div>
    </form>
</div>
<table class="layui-hide" id="test" lay-filter="test"></table>
<script type="text/html" id="toolbarDemo">


    <form class="layui-form " action="">
        <div class="layui-form-item" style="margin-left: 15px;">
            <div class="layui-btn-group">
                <button type="button" class="layui-btn" lay-event="add">增加</button>
                <button type="button" class="layui-btn" lay-event="del">删除</button>
            </div>
        </div>

    </form>
</script>

<script src="${base}/static/win10/component/layui/layui.js?t=1"></script>
<script src="${base}/static/win10/js/jquery-1.8.3.min.js?t=1"></script>
<script>
    layui.use(['table'], function () {

        // 设置全局变量以保存选中行信息
        var ids = new Array();
        // 保存当前页全部数据id，点击全选时使用
        var tableIds = new Array();


        var table = layui.table;
        table.render({
            skin: 'line' //行边框风格
            , even: true //开启隔行背景
            , elem: '#test'
            , url: base + '/papersstar/questionnaire/admin/findQuestionnaireByPage'
            , toolbar: '#toolbarDemo'
            , cols: [[
                {type: 'checkbox'},
                {field: 'id', title: '问卷id'},
                {field: 'questionnaireTitle', title: '问卷标题'},
                {field: 'userId', title: '用户ID '},
                {field: 'createTime', title: '创建时间'},
                {field: 'updateTime', title: '更新时间'},
                {field: 'delFlag', title: '0正常  1停用'},
                {field: 'status', title: '0未发布/1发布/2停用'},
                {field: 'questionnaireType', title: '问卷类型/1调查/2考试'}
            ]]
            , page: true
            , id: 'testReload'
            , done: function (res) {

                // 设置当前页全部数据id到全局变量
                tableIds = res.data.map(function (value) {

                    return value.id;
                });


                // 设置当前页选中项
                $.each(res.data, function (idx, val) {


                    if (ids.indexOf(val.id) > -1) {

                        val["LAY_CHECKED"] = 'true';

                        //找到对应数据改变勾选样式，呈现出选中效果
                        let index = val['LAY_TABLE_INDEX'];


                        $('tr[data-index=' + index + '] input[type="toolbar"]').click();
                        form.render('toolbar'); //刷新toolbar选择框渲染
                    }
                });
                // 获取表格勾选状态，全选中时设置全选框选中
                var checkStatus = table.checkStatus('test');
                if (checkStatus.isAll) {
                    $('.layui-table-header th[data-field="0"] input[type="toolbar"]').prop('checked', true);
                    form.render('toolbar'); //刷新toolbar选择框渲染
                }
            }
        });
        //搜索
        var $ = layui.$, active = {
            reload: function () {
                //执行重载
                table.reload('testReload', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , where: {
                        questionnaireTitle: $("#questionnaireTitle").val(),
                        userId: $("#userId").val(),
                        createTime: $("#createTime").val(),
                        updateTime: $("#updateTime").val(),
                        delFlag: $("#delFlag").val(),
                        status: $("#status").val(),
                        questionnaireType: $("#questionnaireType").val()
                    }
                }, 'data');
            }
        };
        $('.demoTable .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });


        //头工具栏事件
        table.on('toolbar(test)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id); //获取选中行状态
            var data = checkStatus.data;  //获取选中行数据

            var idList = []
            for (let i = 0; i < data.length; i++) {
                idList.push(data[i].id)
            }


            switch (obj.event) {
                case 'add':
                    //请继续补充以下逻辑
                    window.location.href = base + "/papersstar/questionnaire/admin/addQuestionnaireHtml"
                    break;
                case 'edit':
                    if (data.length === 0) {
                        layer.alert("请选择一条数据")
                        return;
                    }
                    //请继续补充以下逻辑
                    window.location.href = base + "/papersstar/questionnaire/admin/editQuestionnaireHtml?id=" + data[0].id
                    break;
                case 'show':
                    if (data.length === 0) {
                        layer.alert("请选择一条数据")
                        return;
                    }
                    //请继续补充以下逻辑
                    window.location.href = base + "/papersstar/questionnaire/admin/showQuestionnaireHtml?id=" + data[0].id
                    break;
                case 'del':
                    if (data.length === 0) {
                        layer.alert("请选择一条数据")
                        return;
                    }
                    layer.confirm('真的要删除吗?', function (index) {
                        $.ajax({
                            type: "POST",
                            url: base + '/papersstar/questionnaire/admin/delQuestionnaire',
                            data: {"datas": idList},
                            dataType: "json",
                            success: function (data) {
                                if (data.code === 200) {
                                    layer.msg(data.message)
                                    setTimeout(function () {
                                        window.location.reload();
                                    }, 500)
                                } else {
                                    layer.msg(data.message)
                                }
                            }
                        });
                    });
                    break;
            }
            ;
        });
        //监听行双击事件
        table.on('rowDouble(test)', function (obj) {
            var data = obj.data;
            window.location.href = base + "/papersstar/questionnaire/admin/showQuestionnaireHtml?id=" + data.id
        });
    })
</script>
</body>
</html>